﻿using System;
using System.Globalization;
using System.Web;

/// <summary>
/// Description for Currency Generator
/// </summary>
public class CurrencyGenrator
{
    /// <summary>
    /// Initializes a new instance of the <see cref="CurrencyGenrator"/> class.
    /// </summary>
    public CurrencyGenrator()
    {
        // TODO: Add constructor logic here
    }

    /// <summary>
    /// Resolves the culture.
    /// </summary>
    /// <returns>CultureInfo</returns>
    public static CultureInfo ResolveCulture()
    {
        string[] languages = HttpContext.Current.Request.UserLanguages;
        if (languages == null || languages.Length == 0)
        {
            return null;
        }

        try
        {
            string language = languages[0].ToLowerInvariant().Trim();
            return CultureInfo.CreateSpecificCulture(language);
        }
        catch (ArgumentException)
        {
            return null;
        }
    }

    /// <summary>
    /// Convertes the amount based on the Culture that we provide.
    /// </summary>
    /// <param name="amount">The amount.</param>
    /// <returns></returns>
    public static string CurrencyFormat(string amount)
    {
        if (amount != "" && amount != string.Empty)
        {
            string culture = System.Globalization.CultureInfo.CurrentUICulture.ToString();            
            decimal currency = Convert.ToDecimal(amount);
            return currency.ToString("C2", CultureInfo.CreateSpecificCulture(culture));
        }
        return "";
    }
}
